package com.common.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @classDescription:
 * @author:xiayingjie
 * @createTime:16/8/17
 */
@Service
public class MailService {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Autowired
    TemplateEngine templateEngine;

    public void sendMail() throws MessagingException {
        Context context=new Context();
        context.setVariable("myname", "xiayingjie");

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);

        helper.setFrom("jiege82000@163.com");
        helper.setTo("287658708@qq.com");
        helper.setSubject("这是测试");

        String emailText=templateEngine.process("mail.html",context);

        helper.setText(emailText,true);

        mailSender.send(mimeMessage);
    }
}
